package bw.hackathon.ub;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class CompleteTest extends Activity {
	
	private ProgressDialog pDialog;
	private Integer result;
	private Integer Pid;
	private Integer count;
	private TextView Error;
	JSONParser jsonParser = new JSONParser();
	private static String url_create_product = "http://www.tsbayford.com/android_connect/send_test.php";
	private static final String TAG_SUCCESS = "success";
	Bundle extras;

	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.last_page);
		Bundle extras = getIntent().getExtras();
		

	      if(extras!=null)
	      {
	    	  count=(extras.getInt("count"))+1;
	    	  result=extras.getInt("result");
	    	  Pid=extras.getInt("Pid");
	      }
		Button Complete = (Button) findViewById(R.id.done_button);
		Complete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new sendResults().execute();

			}
		});
		
	}
	
	class sendResults extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(CompleteTest.this);
			pDialog.setMessage("Sending Test Results..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Creating product
		 * */	
	protected String doInBackground(String... args) {

		// TODO Auto-generated method stub
			List<NameValuePair> args1 = new ArrayList<NameValuePair>();
			args1.add(new BasicNameValuePair("Result", result.toString()));
			args1.add(new BasicNameValuePair("P_Id", Pid.toString()));
		// getting JSON Object
		// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(url_create_product,
					"POST", args1);
		// check log cat for response
			Log.d("Create Response", json.toString());

		// check for success tag
		try {
			int success = json.getInt(TAG_SUCCESS);

			if (success == 1) {
				// successfully created product
				Intent i = new Intent(getApplicationContext(), TestReady.class);
				i.putExtra("message", "Test Completed");
				startActivity(i);
				
				// closing this screen
				finish();
			} else {
				// failed to create product
				Intent i1 = new Intent(getApplicationContext(), TestReady.class);
				i1.putExtra("message", "Connection Failure Please Try Again");
				startActivity(i1);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * After completing background task Dismiss the progress dialog
	 * **/
	protected void onPostExecute(String file_url) {
		// dismiss the dialog once done
		pDialog.dismiss();
	}

	}
}
